/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * URIResourceIDDialog.java
 *
 * Created on 16-mag-2011, 12.49.55
 */
package it.txt.access.capability.spi.resource.amqp.dialog;

import it.txt.access.capability.spi.resource.amqp.model.AMQPResourceIDModel;
import it.txt.access.capability.spi.resource.model.ICapabilityResourceIDModel;
import it.txt.access.capability.spi.resource.visual.ICapabilityResourceIDDialog;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author seccia
 */
public class AMQPResourceIDDialog extends ICapabilityResourceIDDialog {
    
    private static final long serialVersionUID = -6175493372993151914L;
    private StringBuilder errorMessage;

    /** Creates new form URIResourceIDDialog */
    public AMQPResourceIDDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setTitle("Capability :: Plugin :: Resource :: AMQP");
        addFieldContentUpdateListener(host_txt);
        addFieldContentUpdateListener(service_name_txt);
        addFieldContentUpdateListener(this.uriNamespaceText);
        addFieldContentUpdateListener(uriPatterntext);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        scheme_lbl = new javax.swing.JLabel();
        uriAuthorityLabel = new javax.swing.JLabel();
        host_txt = new javax.swing.JTextField();
        uriServiceLabel = new javax.swing.JLabel();
        uriNamespaceLabel = new javax.swing.JLabel();
        uriNamespaceText = new javax.swing.JTextField();
        uriPatternLabel = new javax.swing.JLabel();
        uriPatterntext = new javax.swing.JTextField();
        scheme_txt = new javax.swing.JTextField();
        service_name_txt = new javax.swing.JTextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("AMQP Resource URI"));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resource-bundles/form_labels"); // NOI18N
        scheme_lbl.setText(bundle.getString("scheme_lbl")); // NOI18N

        uriAuthorityLabel.setText(bundle.getString("host_lbl")); // NOI18N

        host_txt.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red));

        uriServiceLabel.setText(bundle.getString("service_lbl")); // NOI18N

        uriNamespaceLabel.setText(bundle.getString("namespace_lbl")); // NOI18N

        uriNamespaceText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        uriPatternLabel.setText(bundle.getString("pattern_lbl")); // NOI18N

        uriPatterntext.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red));

        scheme_txt.setEditable(false);
        scheme_txt.setText("amqp");

        service_name_txt.setText("ENS");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(uriNamespaceLabel)
                    .addComponent(uriPatternLabel)
                    .addComponent(uriServiceLabel)
                    .addComponent(uriAuthorityLabel)
                    .addComponent(scheme_lbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(host_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(uriPatterntext, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(uriNamespaceText, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(scheme_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(service_name_txt, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scheme_lbl)
                    .addComponent(scheme_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uriAuthorityLabel)
                    .addComponent(host_txt))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uriServiceLabel)
                    .addComponent(service_name_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uriNamespaceLabel)
                    .addComponent(uriNamespaceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uriPatternLabel)
                    .addComponent(uriPatterntext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 289, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (validateForm())
            doClose(RET_OK);
        else {
            JOptionPane.showMessageDialog(this,
                    errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField host_txt;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel scheme_lbl;
    private javax.swing.JTextField scheme_txt;
    private javax.swing.JTextField service_name_txt;
    private javax.swing.JLabel uriAuthorityLabel;
    private javax.swing.JLabel uriNamespaceLabel;
    private javax.swing.JTextField uriNamespaceText;
    private javax.swing.JLabel uriPatternLabel;
    private javax.swing.JTextField uriPatterntext;
    private javax.swing.JLabel uriServiceLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public ICapabilityResourceIDModel getModel() {
        AMQPResourceIDModel iDModel = new AMQPResourceIDModel();
        iDModel.setScheme(this.scheme_txt.getText());
        iDModel.setAuthority(this.host_txt.getText());
        iDModel.setService(this.service_name_txt.getText());
        iDModel.setNamespace(this.uriNamespaceText.getText());
        iDModel.setPattern(this.uriPatterntext.getText());
        return iDModel;
    }
    
    public boolean validateForm () {
        errorMessage = new StringBuilder();
        
        StringBuilder missingFields = new StringBuilder();
        
        isMissingTextField(host_txt, missingFields);
        isMissingTextField(service_name_txt, missingFields);
        isMissingTextField(uriNamespaceText, missingFields);
        isMissingTextField(uriPatterntext, missingFields);
        
                
        errorMessage = new StringBuilder();
        if (missingFields.length() > 0) {
            missingFields.insert(0, "The following fields are empty:\n");
            errorMessage.append(missingFields);
        }
        if (errorMessage.length() > 0)
            return false;
        else
            return true;
    }
    
    private boolean isMissingTextField (JTextField textBox, StringBuilder missingFieldMessage) {
        if (textBox.getText().equals("")) {
            if (missingFieldMessage.length() > 0)
                missingFieldMessage.append(", ").append(textBox.getName());
            else
                missingFieldMessage.append(textBox.getName());
            return true;
        }
        return false;
    }
    
    private void addFieldContentUpdateListener(final javax.swing.JTextField textBox) {

        textBox.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                hihglightFieldBorder(e);
            }

            public void removeUpdate(DocumentEvent e) {
                hihglightFieldBorder(e);
            }

            public void changedUpdate(DocumentEvent e) {
                hihglightFieldBorder(e);
            }

            private void hihglightFieldBorder(DocumentEvent e) {
                if (textBox.getText().equals("")) {
                    textBox.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                } else {
                    textBox.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                }
            }
        });
    }
}
